package com.penghaisoft.microwms.base.web.data.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.penghaisoft.microwms.base.web.data.service.IBaseDictionaryService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping(value = "/dic")
public class DictionaryController extends BaseController {

	@Autowired
	private IBaseDictionaryService baseDictionaryService;

	@Autowired
	private StringRedisTemplate redisTemplate;// spring-data-redis

	/**
	 * @Title create
	 * @Description 新增记录
	 * @author luoteng
	 * @date 2017年6月9日:下午5:19:35
	 * @param baseDictionary
	 * @return TransResult
	 */
	@PostMapping("c")
	public TransResult create(BaseDictionary baseDictionary) {
		baseDictionary.preInsert();
		Resp resp = baseDictionaryService.create(baseDictionary);
		dicReload();
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 *
	 * @param
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			BaseDictionary baseDictionary) {
		Pager<BaseDictionary> result = baseDictionaryService
				.findListByCondition(page, rows, baseDictionary);
		return success(result);
	}
	/**
	 * 查询盘点类型
	 *
	 * @param baseDictionary
	 * @return
	 */
	@PostMapping("get/accounting_type")
	public TransResult queryAccttingType(BaseDictionary baseDictionary) {
		List<BaseDictionary> result = baseDictionaryService.queryAccttingType(baseDictionary);
		return success(result);
	}
	
	/**
	 * 查询单条
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		BaseDictionary baseDictionary = baseDictionaryService.findById(id);
		return success(baseDictionary);
	}

	/**
	 * @Title update
	 * @Description 更新
	 * @author luoteng
	 * @date 2017年6月9日:下午5:18:39
	 * @param baseDictionary
	 * @return TransResult
	 */
	@PostMapping("u")
	public TransResult update(BaseDictionary baseDictionary) {
		baseDictionary.preUpdate();
		Resp resp = baseDictionaryService.update(baseDictionary);
		dicReload();
		return fromResp(resp);
	}
	
	/** 
	 * @Title batchUpdate
	 * @Description 批量更新
	 * @author luoteng
	 * @date 2017年6月21日:下午4:54:03
	 * @param list
	 * @return TransResult
	 */
	@PostMapping("u/batch")
	public TransResult batchUpdate(@RequestBody List<BaseDictionary> list) {
		if(!list.isEmpty()){
			//先删除字典，再新增
			baseDictionaryService.deleteByPrimaryKeys(list.get(0).getDicType());
			
			for(BaseDictionary baseDictionary : list){
				baseDictionary.setLastModifiedBy(getLoginName());
				baseDictionary.setGmtModified(new Date());
				baseDictionary.preInsert();
				baseDictionaryService.create(baseDictionary);
			}
			dicReload();
		}
		
		return success();
	}

	/**
	 * @Title queryAll
	 * @Description 查询满足条件字典明细
	 * @author luoteng
	 * @date 2017年6月9日:下午5:19:11
	 * @param baseDictionary
	 * @return TransResult
	 */
	@PostMapping("list/all")
	public TransResult queryAll(BaseDictionary baseDictionary) {
		List<BaseDictionary> list = baseDictionaryService
				.queryAllByCondition(baseDictionary);
		return success(list);
	}

	/**
	 * @Title queryDicType
	 * @Description 查询满足条件的字典种类
	 * @author luoteng
	 * @date 2017年6月9日:下午3:39:15
	 * @param baseDictionary
	 * @return TransResult
	 */
	@PostMapping("list/dicType")
	public TransResult queryDicType(BaseDictionary baseDictionary) {
		List<BaseDictionary> list = baseDictionaryService
				.queryDicTypeByCondition(baseDictionary);
		return success(list);
	}

	/**
	 * @Title queryByTypId
	 * @Description 从redis缓存查询下拉框字典
	 * @author luoteng
	 * @date 2017年6月9日:下午3:39:07
	 * @param dicType
	 * @return TransResult
	 */
	@GetMapping("get/{dicType}")
	public TransResult queryRedisByTypId(@PathVariable String dicType) {
		List<BaseDictionary> list = JSONArray.parseArray(redisTemplate.opsForValue().get(Constant.DICTIONARY_DICTYPE + dicType),BaseDictionary.class);
		if(list.size() == 0 || list == null){
			dicReload();
			list = JSONArray.parseArray(redisTemplate.opsForValue().get(Constant.DICTIONARY_DICTYPE + dicType),BaseDictionary.class);
		}
		return success(list);
	}
	
	public void dicReload() {
		//只加载有效的字典
		BaseDictionary tmp = new BaseDictionary();
		tmp.setActiveFlag("1");
		List<BaseDictionary> list = baseDictionaryService.queryAllByCondition(tmp);
		Map<String, List> map = new HashMap<String, List>();
		for(BaseDictionary baseDictionary : list){
			String dicType = baseDictionary.getDicType();
			List<BaseDictionary> tmpList;
			if(map.containsKey(dicType)){
				tmpList = map.get(dicType);
				
			}else{
				tmpList = new ArrayList<BaseDictionary>();
			}
			tmpList.add(baseDictionary);
			map.put(dicType, tmpList);
		}

		redisTemplate.delete(Constant.DICTIONARY_DICTYPE + "*");

		for (String key:map.keySet()){
			redisTemplate.opsForValue().set(Constant.DICTIONARY_DICTYPE + key, JSONObject.toJSONString(map.get(key)));
		}
	}


}
